$(function () {
    $("#jqGrid").jqGrid({
        url: '../hotcountry/list',
        datatype: "json",
        colModel: [			
			//{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '国家', name: 'countryName', index: 'country_name', width: 80 }, 			
			{ label: '图片', name: 'imgName', index: 'img_name', width: 80 , formatter : function(cellValue, options, rowObject) {
				if(cellValue) {
					return '<a href="javascript:vm.show(\'' + rowObject.img + '\');">' + cellValue + '</a>';
				} else {
					return '';
				}
			}}, 			
			{ label: '介绍', name: 'tip', index: 'tip', width: 80 }, 			
			{ label: '序号', name: 'sortNum', index: 'sort_num', width: 80 }	,
			{ label: 'img', name: 'img', width: 80 , hidden:true}		
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.loadCountry();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		url : null,
		showList: true,
		title: null,
		q:{
			key: null
		},
		country : [],
		hotCountry: {
			imgName : null
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.hotCountry = {imgName : null};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.hotCountry.id == null ? "../hotcountry/save" : "../hotcountry/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.hotCountry),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../hotcountry/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../hotcountry/info/"+id, function(r){
				console.dir(r);
                vm.hotCountry = r.hotCountry;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'key': vm.q.key},
                page:page
            }).trigger("reloadGrid");
		},
		loadCountry : function() {
			$.ajax({
				type: "POST",
			    url: "../qygj/country/queryAll",
                contentType: "application/json",
			    success: function(r){
					if(r.code == 0){
						vm.country = r.country;
					}
				}
			});
		},
		showImg : function(event) {
			showFile(true, vm.setImg);
		},
		setImg : function(name, url) {
			vm.hotCountry.imgName = name;
		    vm.hotCountry.img = url;
		},
		show : function(url) {
			vm.url = '../f/d/' + url;
			layer.open({
				type:1,
				title: "查看图片",
				area: ['550px', '470px'],
				shadeClose: false,
				shade: 0.000001,
				anim : 4,
				resize : true,
				content: $("#show"),
				btn: ['确认'],
				btn1: function (index) {
					layer.close(index);
	            }
			});
		}
	}
});